package com.qs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.constant.CacheConstant;
import com.qs.constant.UserConstant;
import com.qs.exception.ServiceException;
import com.qs.model.dto.BindDto;
import com.qs.model.dto.RegisterDto;
import com.qs.model.entity.User;
import com.qs.response.Response;
import com.qs.response.ResponseEnum;
import com.qs.service.IUserService;
import com.qs.utils.EncryptUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: LoginController
 * Package: com.qs.controller
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/3/31 - 11:49
 * @Version: v1.0
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/register")
@RequiredArgsConstructor
public class RegisterController {

    private final IUserService userService;

    private final RedisTemplate redisTemplate;

    /**
     * 注册接口
     * @param registerDto
     * @return
     */
    @PostMapping("/front")
    public Response register(@RequestBody RegisterDto registerDto) {
        //判断用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(registerDto.getNickName()),"nick_name",registerDto.getNickName());
        User searchUser = userService.getOne(wrapper);
        if(null != searchUser) {
            throw new ServiceException(ResponseEnum.USER_EXITS);
        }
        //密码加密
        String encryptPassword = EncryptUtils.MD5Password(registerDto.getPassword());
        User user = new User();
        user.setNickName(registerDto.getNickName());
        user.setPassword(encryptPassword);
        //将用户角色默认设置为普通用户
        user.setRole(UserConstant.ROLE_USER);
        userService.save(user);
        return Response.success();
    }

    /**
     * 绑定手机号
     * @param bindDto
     * @return
     */
    @PostMapping("/front/bind")
    public Response bind(@RequestBody BindDto bindDto) {
        //判断用户是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(bindDto.getNickName()),"nick_name",bindDto.getNickName());
        User searchUser = userService.getOne(wrapper);
        if(null == searchUser) {
            throw new ServiceException(ResponseEnum.USER_NOT_EXITS);
        }
        //检验验证码是否过期
        String code = (String) redisTemplate.opsForValue().get(CacheConstant.MESSAGE_CODE + bindDto.getPhone());
        if(StringUtils.isEmpty(code)){
            throw new ServiceException(ResponseEnum.CODE_EXPIRE);
        }
        //对比验证码是否正确
        if(!bindDto.getCode().equalsIgnoreCase(code)){
            throw new ServiceException(ResponseEnum.CODE_ERROR);
        }
        searchUser.setPhone(bindDto.getPhone());
        userService.updateById(searchUser);
        return Response.success();
    }
}
